
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'dart:io';


class MyMapView extends StatefulWidget{

  @override
  createState ()=> new MapView();
}

class MapView extends State<MyMapView> {

  static const BasicMessageChannel<String> _basicMessageChannel = const BasicMessageChannel('messageChannelName', StringCodec());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('地图'),
      ),
      body: Center(
          child: buildMapView()
      ),
    );
  }

  Widget buildMapView(){
    if(Platform.isAndroid){
      return new Container(
        child: new AndroidView(viewType: "mapview",
          // 在创建 AndroidView 的同时，可以传递参数
          creationParams: <String, dynamic>{"coordinates": "[{\"lng\":\"121.28343\",\"lat\":\"31.325402\",\"name\":\"骏丰玲珑坊\"},"
              "{\"lng\":\"121.50642\",\"lat\":\"31.245625\",\"name\":\"汤臣一品\"}]"},
          // 用来编码 creationParams 的形式，可选 [StandardMessageCodec], [JSONMessageCodec], [StringCodec], or [BinaryCodec]
          // 如果存在 creationParams，则该值不能为null
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    }else{
      return new Container(
        child: new UiKitView(viewType: "mapview",
          // 在创建 AndroidView 的同时，可以传递参数
          creationParams: <String, dynamic>{"coordinates": "[{\"lng\":\"121.28343\",\"lat\":\"31.325402\",\"name\":\"骏丰玲珑坊\"},"
              "{\"lng\":\"121.50642\",\"lat\":\"31.245625\",\"name\":\"汤臣一品\"}]"},
          // 用来编码 creationParams 的形式，可选 [StandardMessageCodec], [JSONMessageCodec], [StringCodec], or [BinaryCodec]
          // 如果存在 creationParams，则该值不能为null
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    }
  }

  @override
  void initState() {
    _basicMessageChannel.setMessageHandler((String message) => Future<String>((){
      setState(() {
      });
      return "received message from Native:" + message;
    }));

    super.initState();
  }

}